# -*- coding: utf-8 -*-
from functools import partial

from sqlalchemy.exc import IntegrityError

from common.lottery.cyclical.abstract import activity as base
from common.utils.decorator import sql_wrapper
from common.lottery import LOTTERY_TYPE


fullfill_result = sql_wrapper(
    partial(base.fullfill_result, LOTTERY_TYPE.FF_KS))


get_latest_term = sql_wrapper(
    partial(base.get_latest_term, LOTTERY_TYPE.FF_KS))


get_activity_by_term = sql_wrapper(
    partial(base.get_activity_by_term, LOTTERY_TYPE.FF_KS))


stop_term = sql_wrapper(
    partial(base.stop_term, LOTTERY_TYPE.FF_KS))


create_new_term = sql_wrapper(
    partial(base.create_new_term, LOTTERY_TYPE.FF_KS))


get_stats_by_term = sql_wrapper(
    partial(base.get_stats_by_term, LOTTERY_TYPE.FF_KS))

get_activity_stats = sql_wrapper(
    partial(base.get_activity_stats, LOTTERY_TYPE.FF_KS))


@sql_wrapper
def insert_stats(term, number, last_stats):
    trend, save = base.generate_trend(
        LOTTERY_TYPE.FF_KS, term, number, last_stats)
    trend, save = calc_miss(last_stats, number, save, trend)

    if save:
        try:
            trend.save()
        except IntegrityError:
            pass

    return trend


def calc_miss(last_stats, number, save, trend):
    def calc_func(last, i):
        return str(int(last[i]) + 1) if last[i] != 'x' else 'x'

    uniques = set(number)
    # 和值 3~18
    hit = sum([int(k) for k in number])
    last = last_stats.miss_1.split(',')
    for i in xrange(len(last)):
        last[i] = calc_func(last, i)
    last[hit - 3] = '0'
    if 'x' in last:
        save = False
    trend.miss_1 = ','.join(last)
    # 三同号单选/通选 111 ~ 666
    last = last_stats.miss_2.split(',')
    for i in xrange(len(last)):
        last[i] = calc_func(last, i)
    if len(uniques) == 1:
        last[int(number[0]) - 1] = '0'
    if 'x' in last:
        save = False
    trend.miss_2 = ','.join(last)
    # 二同号单选
    lasts = last_stats.miss_4.split('|')
    current = [0] * 2
    for i, v in enumerate(lasts):
        last = v.split(',')
        for j in xrange(len(last)):
            last[j] = calc_func(last, j)
        current[i] = last
    if len(uniques) == 2:
        dup = 0
        for n in number:
            dup ^= int(n)
        single = dup
        for n in uniques:
            dup ^= int(n)
        current[0][dup - 1] = '0'
        current[1][single - 1] = '0'
    if 'x' in current[0] or 'x' in current[1]:
        save = False
    trend.miss_4 = '|'.join([','.join(c) for c in current])
    # 二同号复选
    last = last_stats.miss_5.split(',')
    for i in xrange(len(last)):
        last[i] = calc_func(last, i)
    if len(uniques) == 1:
        last[int(number[0]) - 1] = '0'
    elif len(uniques) == 2:
        last[dup - 1] = '0'
    if 'x' in last:
        save = False
    trend.miss_5 = ','.join(last)
    # 三不同号/二不同号
    last = last_stats.miss_6.split(',')
    for i in xrange(len(last)):
        last[i] = calc_func(last, i)
    for n in number:
        last[int(n) - 1] = '0'
    if 'x' in last:
        save = False
    trend.miss_6 = ','.join(last)
    # 三连号
    last = last_stats.miss_8
    if number in ('123', '234', '345', '456'):
        trend.miss_8 = '0'
    else:
        trend.miss_8 = str(int(last) + 1) if last != 'x' else 'x'
    if trend.miss_8 == 'x':
        save = False

    # 任一
    last = last_stats.miss_9.split(',')
    for k in xrange(6):
        if str(k + 1) in number:
            last[k] = '0'
        else:
            last[k] = calc_func(last, k)
    if 'x' in last:
        save = False
    trend.miss_9 = ','.join(last)
    return trend, save
